package com.sugon.modules.tools.service.impl;

import cn.hutool.core.util.StrUtil;
import com.sugon.dao.ToolsListDao;
import com.sugon.entity.SysDeptEntity;
import com.sugon.entity.ToolsListEntity;
import com.sugon.entity.ToolsScrapEntity;
import com.sugon.modules.base.consts.SysApproveStatusConst;
import com.sugon.modules.base.model.dto.HandleApproveResult;
import com.sugon.modules.base.model.vo.ProcessProgressConst;
import com.sugon.modules.base.service.impl.AbstractErmsBaseApproveManager;
import com.sugon.modules.sys.consts.ProcessProgressStatus;
import com.sugon.modules.tools.service.IToolsManager;
import com.sugon.service.SysDeptService;
import com.sugon.service.ToolsListService;
import com.sugon.service.ToolsScrapService;
import com.sugon.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author ： YuXD
 * @description： 工装业务Manager实现类
 * @date ： 2020-08-12 上午 10:58
 * @version: 1.0
 */
@Service
public class ToolsManagerImpl extends AbstractErmsBaseApproveManager implements IToolsManager {

    @Autowired
    private SysDeptService deptService;
    @Autowired
    private ToolsListDao toolsListDao;
    @Autowired
    private ToolsListService toolslistService;
    @Autowired
    private ToolsScrapService toolsScrapService;

    /**
     * handleWorkshopApprove
     *
     * @param approveItemId 审批事项ID
     * @param approveStatus 审批状态
     * @param remark        审批备注
     * @return
     */
    @Override
    public HandleApproveResult handleWorkshopApprove(String approveItemId, Integer approveStatus, String remark) {
        HandleApproveResult handleApproveResult = new HandleApproveResult();
        ToolsListEntity toolslist = toolsListDao.queryObject(approveItemId);
        org.springframework.util.Assert.notNull(toolslist, "工装清单不存在，请确认！");
        // 校验规程进度
        org.springframework.util.Assert.isTrue(ProcessProgressConst.CJ_PE_APPROVING.equals(toolslist.getProgress()), "只可以审核[PE审核]的工装清单！");
        handleApproveResult.setProcessProgressStatus(ProcessProgressStatus.ING);
        handleApproveResult.setTitle(toolslist.getName());
        // 如果审核通过，则更新进度
        if (approveStatus == SysApproveStatusConst.PASSED) {
            handleApproveResult.setProcessProgressStatus(ProcessProgressStatus.END);
            toolslist.setProgress(ProcessProgressConst.PASSED);
        } else if (approveStatus == SysApproveStatusConst.NOT_PASSED) {
            handleApproveResult.setProcessProgressStatus(ProcessProgressStatus.ING);
            toolslist.setProgress(ProcessProgressConst.NOT_PASSED);
        }
        int rows = toolslistService.update(toolslist);
        if (rows > 0) {
            handleApproveResult.setUserId(toolslist.getUpdateUser());
        }
        return handleApproveResult;
    }

    /**
     * addToolsScrapRecord
     *
     * @param toolsScrap
     * @return
     */
    @Override
    public boolean addToolsScrapRecord(ToolsScrapEntity toolsScrap) {
        // 设置工装所属车间ID和车间名称
        String workshop = ShiroUtils.getWorkshop();
        if (StrUtil.isNotEmpty(workshop)) {
            toolsScrap.setFarmId(workshop);
            SysDeptEntity deptEntity = deptService.queryObject(workshop);
            if (deptEntity != null) {
                toolsScrap.setFarmName(deptEntity.getName());
            }
        }
        // 设置工装类别和类别名称
        ToolsListEntity tb = toolsListDao.loadByCode(toolsScrap.getOnlyMark());
        if (tb != null) {
            toolsScrap.setToolsType(tb.getToolsType());
            toolsScrap.setToolsTypeName(tb.getTypeName());
        }
        return toolsScrapService.save(toolsScrap) > 0;
    }
}
